package com.cmcc.linlaker.ui.mine;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.bean.BaseBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.adapter.MessageAdapter;
import com.cmcc.linlaker.ui.mine.bean.MessageBean;
import com.cmcc.linlaker.ui.mine.bean.MessageDetailBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageDetailActivity extends AppCompatActivity {
    private MessageBean.MessageListBean msgBean;
    private String msgId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_detail);

        BarUtils.initStatusBar(this);

        msgBean = (MessageBean.MessageListBean) getIntent().getSerializableExtra("msgBean");
        msgId = getIntent().getStringExtra("msgId");

        initView();
    }

    private TextView text_send_name, text_send_date, text_content;
    private EditText edit_content;
    private TextView text_replay_content;

    private void initView() {
        text_replay_content = findViewById(R.id.text_replay_content);
        edit_content = findViewById(R.id.edit_content);
        edit_content.setFilters(new InputFilter[]{inputFilter});

        text_send_name = findViewById(R.id.text_send_name);
        text_send_date = findViewById(R.id.text_send_date);
        text_content = findViewById(R.id.text_content);

        if (msgBean != null) {
            text_send_name.setText(msgBean.sysMsgInner.sendUserName);
            text_send_date.setText(msgBean.sysMsgInner.sendDate);
            text_content.setText(msgBean.sysMsgInner.msgContent);

            if (msgBean.sysMsgInner.sysMsgInnerRecord.readStatus.equals("1")) {
                //已读
                findViewById(R.id.layout_reply).setVisibility(View.GONE);
                findViewById(R.id.layout_reply_content).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.layout_reply).setVisibility(View.VISIBLE);
                findViewById(R.id.layout_reply_content).setVisibility(View.GONE);
            }
            readMessage(msgBean.id);
            getMsgDetail(msgBean.id);
        } else {
            readMessage(msgId);
            getMsgDetail(msgId);
        }


    }


    /**
     * 读取消息
     */
    private void readMessage(String id) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("id", id);
        params.put("readStatus", "1");

        helper.sendPOSTRequest(ApiDefine.MSG_READ, BaseBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
            }
        });
    }

    /**
     * 消息详情
     */
    private void getMsgDetail(String id) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String url = ApiDefine.MSG_DETAIL + "/" + id;

        helper.sendGETRequest(url, MessageDetailBean.class, true, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    MessageDetailBean bean = (MessageDetailBean) data;

                    if (!TextUtils.isEmpty(msgId)) {
                        text_send_name.setText(bean.data.sysMsgInner.sendUserName);
                        text_send_date.setText(bean.data.sysMsgInner.sendDate);
                        text_content.setText(bean.data.sysMsgInner.msgContent);

                        if (bean.data.sysMsgInner.sysMsgInnerRecord.readStatus.equals("1")) {
                            //已读
                            findViewById(R.id.layout_reply).setVisibility(View.GONE);
                            findViewById(R.id.layout_reply_content).setVisibility(View.VISIBLE);
                        } else {
                            findViewById(R.id.layout_reply).setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_reply_content).setVisibility(View.GONE);
                        }
                    }

                    List<MessageDetailBean.FeedbackBean> feedbackBeanList = bean.data.feedbackList;
                    StringBuffer stringBuffer = new StringBuffer();
                    for (MessageDetailBean.FeedbackBean feedbackBean : feedbackBeanList) {
                        stringBuffer.append(feedbackBean.feedback + "\n");
                    }
                    text_replay_content.setText(stringBuffer.toString());
                }
            }
        });
    }

    public void back(View view) {
        finish();
    }

    /**
     * 提交回復
     */
    public void confirm(View view) {
        String content = edit_content.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showText(this, "请输入回复内容");
            return;
        }

        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("msgInnerId", msgBean.id);
        params.put("userId", SPHelper.getUid());
        params.put("feedback", content);

        helper.sendPOSTRequest(ApiDefine.MSG_FEEDBACK, BaseBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    BaseBean bean = (BaseBean) data;
                    if (bean.code.equals("0")) {
                        ToastUtils.showText(MessageDetailActivity.this, "回复成功");

                        finish();
                    } else {
                        ToastUtils.showText(MessageDetailActivity.this, bean.msg);
                    }
                }
            }
        });
    }

    InputFilter inputFilter = new InputFilter() {

        Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\u4E00-\\u9FA5_]");

        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            Matcher matcher = pattern.matcher(charSequence);
            if (!matcher.find()) {
                return null;
            } else {
                ToastUtils.showText(MessageDetailActivity.this, "只能输入汉字,英文，数字");
                return "";
            }

        }
    };

}
